import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_test1/app/base/base_list/base_list_mixin.dart';
import 'package:flutter_test1/db/table/account_table.dart';
import 'package:flutter_test1/helper/dialog/dialog_helper.dart';
import 'package:flutter_test1/helper/gen_isolate/account_generator.dart';
import 'package:flutter_test1/helper/web3_utils.dart';
import 'package:flutter_test1/model/account_model.dart';
import 'package:get/get.dart';

class HomeLogic extends GetxController with BaseListMixin<AccountModel> {

  final accounts = <AccountModel>[].obs;
  final searchRes = <AccountModel>[].obs;
  final ethUp = true.obs;
  final tronUp = true.obs;
  final sortIndex = 0.obs;
  final inSearch = false.obs;
  final running = false.obs;

  TextEditingController searchCtrl = TextEditingController();

  final AccountGenerator accountGenerator1 = AccountGenerator();
  final AccountGenerator accountGenerator2 = AccountGenerator();
  final AccountGenerator accountGenerator3 = AccountGenerator();


  Timer? timer;

  @override
  void onInit() {
    loadData();
    // addSearchListen();
    super.onInit();
  }

  @override
  Future<List<AccountModel>> getData(pageIndex, pageSize) {
    return getAllAccounts();
  }

  @override
  void onListDataChanged() {
    debugPrint('onListDataChanged dataList length: ${dataList.length}');
    accounts.value = dataList;
  }

  Future<List<AccountModel>> getAllAccounts() async {
    if (sortIndex.value == 0) {
      return getEthAccountOrder();
    } else {
      return getTronAccountOrder();
    }
  }

  Future genAccounts() async {
    debugPrint('开启子线程');
    running.value = true;

    accountGenerator1.startGenerating((AccountModel account) {
      debugPrint('1收到账号, 插入数据库');
      AccountTable().insert(account.toJson());
    });
    accountGenerator2.startGenerating((AccountModel account) {
      debugPrint('2收到账号, 插入数据库');
      AccountTable().insert(account.toJson());
    });
    accountGenerator3.startGenerating((AccountModel account) {
      debugPrint('3收到账号, 插入数据库');
      AccountTable().insert(account.toJson());
    });
    timer = Timer.periodic(Duration(seconds: 5), (timer) {
      /// 5s刷新一次数据
      loadData();
    });
  }

  stopGen() async {
    debugPrint('停止产生');
    running.value = false;
    accountGenerator1.stopGenerating();
    accountGenerator2.stopGenerating();
    accountGenerator3.stopGenerating();
    timer?.cancel();
  }

  Future<List<AccountModel>> getEthAccountOrder() async {
    return await AccountTable().getAccountsSorted(ethUp.value ? 'eth_tail ASC' : 'eth_tail DESC');
  }

  Future<List<AccountModel>> getTronAccountOrder() async {
    return await AccountTable().getAccountsSorted(tronUp.value ? 'tron_tail ASC' : 'tron_tail DESC');
  }

  onSearchTap() {
    if (searchCtrl.text.isNotEmpty) {
      // searchCtrl.clear();
      searchRes.clear();
      inSearch.value = true;
      for (var e in accounts) {
        if (e.toJson().toString().contains(searchCtrl.text)) {
          searchRes.add(e);
          debugPrint('包含目标: ${e}');
        }
      }
    } else {
      inSearch.value = false;
      searchRes.clear();
    }
    searchRes.refresh();
  }

  deleteAccount(String key) {
    DialogHelper.showConfirmDialog(
      tag: 'delete_dialog',
      title: '确认删除?',
      onOkTap: () {
        DialogHelper.dismiss(tag: 'delete_dialog');
        AccountTable().deleteAccountByPriKey(key);
        loadData();
        inSearch.value = false;
        searchCtrl.clear();
      }
    );

  }

}
